package cn.iocoder.yudao.module.system.dal.mysql.investment;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.investment.vo.InvestmentReqVO;
import cn.iocoder.yudao.module.system.controller.admin.investment.vo.ParticipationReqVO;
import cn.iocoder.yudao.module.system.controller.app.investment.vo.AppInvestmentPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.investment.SystemInvestment;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Mapper
public interface InvestmentMapper extends BaseMapperX<SystemInvestment> {

    /**
     * 分页查询招商方列表
     *
     * @param reqVO
     * @return
     */
    default PageResult<SystemInvestment> selectPage(InvestmentReqVO reqVO) {
        LambdaQueryWrapperX<SystemInvestment> query = new LambdaQueryWrapperX<>();
        query.likeIfPresent(SystemInvestment::getName, reqVO.getName());
        if (!Objects.isNull(reqVO.getStartTime())) {
            query.betweenIfPresent(SystemInvestment::getStartDate, reqVO.getStartTime()[0], reqVO.getStartTime()[1]);
        }
        query.likeIfPresent(SystemInvestment::getContactPerson, reqVO.getContactPhone());
        query.eqIfPresent(SystemInvestment::getMerchantId, reqVO.getMerchantId());
        query.orderByDesc(SystemInvestment::getId);
        return selectPage(reqVO, new LambdaQueryWrapperX<SystemInvestment>()
                .orderByDesc(SystemInvestment::getId));
    }

    default int updateInvestment(SystemInvestment investment) {
        LambdaUpdateWrapper<SystemInvestment> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SystemInvestment::getId, investment.getId());
        updateWrapper.set(investment.getStatus() != null, SystemInvestment::getStatus, investment.getStatus());
        updateWrapper.set(investment.getMerchantId() != null, SystemInvestment::getMerchantId, investment.getMerchantId());
        updateWrapper.set(investment.getName() != null, SystemInvestment::getName, investment.getName());
        updateWrapper.set(investment.getDescription() != null, SystemInvestment::getDescription, investment.getDescription());
        updateWrapper.set(investment.getDescriptionUrl() != null, SystemInvestment::getDescriptionUrl, investment.getDescriptionUrl());
        updateWrapper.set(investment.getStartDate() != null, SystemInvestment::getStartDate, investment.getStartDate());
        updateWrapper.set(investment.getEndDate() != null, SystemInvestment::getEndDate, investment.getEndDate());
        updateWrapper.set(investment.getContactPerson() != null, SystemInvestment::getContactPerson, investment.getContactPerson());
        updateWrapper.set(SystemInvestment::getContactEmail, investment.getContactEmail());
        updateWrapper.set(SystemInvestment::getContactPhone, investment.getContactPhone());
        updateWrapper.set(investment.getPlanUrl() != null, SystemInvestment::getPlanUrl, investment.getPlanUrl());
        return update(investment, updateWrapper);
    }

    /**
     * 根据招商项目id查询招商方id
     *
     * @param investmentIds
     * @return
     */
    default List<Long> selectMerchantIdsByInvestmentIds(InvestmentReqVO reqVO, List<Integer> investmentIds) {
        if (Objects.isNull(investmentIds) || investmentIds.isEmpty()) {
            return null;
        }
        LambdaQueryWrapperX<SystemInvestment> query = new LambdaQueryWrapperX<>();
        query.likeIfPresent(SystemInvestment::getName, reqVO.getName());
        if (!Objects.isNull(reqVO.getStartTime()) && reqVO.getStartTime().length == 2) {
            query.betweenIfPresent(SystemInvestment::getStartDate, reqVO.getStartTime()[0], reqVO.getStartTime()[1]);
        }
        query.likeIfPresent(SystemInvestment::getContactPerson, reqVO.getContactPhone());
        query.in(SystemInvestment::getId, investmentIds);
        return selectList(query).stream().map(SystemInvestment::getMerchantId).distinct().collect(Collectors.toList());
    }

    /**
     * 根据招商方id查询招商项目id
     *
     * @param reqVO
     * @param merchantId
     * @return
     */
    default List<Integer> selectInvestmentIdsByMerchantId(ParticipationReqVO reqVO, Long merchantId) {
        LambdaQueryWrapperX<SystemInvestment> query = new LambdaQueryWrapperX<>();
        query.orderByDesc(SystemInvestment::getId);
        query.eq(SystemInvestment::getMerchantId, merchantId);
        query.select(SystemInvestment::getMerchantId);
        return selectList(query).stream().map(SystemInvestment::getId).collect(Collectors.toList());
    }

    default PageResult<SystemInvestment> selectPageResult(AppInvestmentPageReqVO pageVO) {
        LambdaQueryWrapperX<SystemInvestment> query = new LambdaQueryWrapperX<SystemInvestment>()
                .likeIfPresent(SystemInvestment::getName, pageVO.getName())
                .likeIfPresent(SystemInvestment::getProduceArea, pageVO.getProduceArea())
                .eqIfPresent(SystemInvestment::getProductType, pageVO.getProductType());
        return selectPage(pageVO, query);
    }

    default PageResult<SystemInvestment> selectByRandom(){
        return selectPage(new PageParam(), new LambdaQueryWrapper<SystemInvestment>().last(" ORDER BY RAND()"));
    }
}
